/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/

package com.genia.toolbox.web.gwt.form.client.widget;

import com.genia.toolbox.web.gwt.basics.client.widget.HyperLinkButton;
import com.genia.toolbox.web.gwt.basics.client.widget.WidgetFactory;
import com.genia.toolbox.web.gwt.form.client.CssNames;
import com.genia.toolbox.web.gwt.form.client.message.Messages;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * widget that add sort button to a wrapp widget.
 */
class WidgetWithSortButtons
    extends Composite
    implements ClickListener
{

  /**
   * the {@link Panel} that will contain the buttons.
   */
  private final Panel buttonsPanel;

  /**
   * the {@link Panel} that will contain the widget.
   */
  private final Panel mainPanel;

  /**
   * the {@link Widget} that must be sortable in its upper panel.
   */
  private final Widget baseWidget;

  /**
   * the {@link VerticalPanel} that contains all the widget that can be sorted.
   */
  private final SortableVerticalPanel sortableVerticalPanel;

  /**
   * the button that must put the current widget in the first position.
   */
  private final HyperLinkButton toFirstPositionButton = WidgetFactory.newHyperLinkButton(Messages.TO_FIRST_POSITION_MESSAGE, CssNames.TO_FIRST_POSITION_BUTTON_CSS_STYLE);

  /**
   * the button that must put the current widget in previous position.
   */
  private final HyperLinkButton upButton = WidgetFactory.newHyperLinkButton(Messages.UP_MESSAGE, CssNames.UP_BUTTON_CSS_STYLE);

  /**
   * the button that must put the current widget in next position.
   */
  private final HyperLinkButton downButton = WidgetFactory.newHyperLinkButton(Messages.DOWN_MESSAGE, CssNames.DOWN_BUTTON_CSS_STYLE);

  /**
   * the button that must put the current widget in the last position.
   */

  private final HyperLinkButton toLastPositionButton = WidgetFactory.newHyperLinkButton(Messages.TO_LAST_POSITION_MESSAGE, CssNames.TO_LAST_POSITION_BUTTON_CSS_STYLE);



  /**
   * constructor.
   * 
   * @param sortableVerticalPanel
   *          the {@link SortableVerticalPanel} that will contain all
   *          {@link WidgetWithSortButtons}
   * @param baseWidget
   *          the widget to wrapp
   */
  public WidgetWithSortButtons(final SortableVerticalPanel sortableVerticalPanel, Widget baseWidget)
  {
    this.baseWidget = baseWidget;
    this.sortableVerticalPanel = sortableVerticalPanel;
    mainPanel = new HorizontalPanel();
    if (sortableVerticalPanel.getSortableMultipleItem().isSortButtonsDispositionVertical()) {
      buttonsPanel = new VerticalPanel();
    }
    else {
      buttonsPanel = new HorizontalPanel();
    }

    toFirstPositionButton.addClickListener(this);
    upButton.addClickListener(this);
    downButton.addClickListener(this);
    toLastPositionButton.addClickListener(this);

    buttonsPanel.add(toFirstPositionButton);
    buttonsPanel.add(upButton);
    buttonsPanel.add(downButton);
    buttonsPanel.add(toLastPositionButton);

    mainPanel.add(baseWidget);
    mainPanel.add(buttonsPanel);
    initWidget(mainPanel);
  }



  /**
   * hide/show the control depending on the position of the widget in the panel.
   * 
   * @param isFirstElement
   *          whether the current element is the first element
   */
  void setFirstElement(boolean isFirstElement)
  {
    toFirstPositionButton.setVisible(!isFirstElement);
    upButton.setVisible(!isFirstElement);
  }



  /**
   * hide/show the control depending on the position of the widget in the panel.
   * 
   * @param isLastElement
   *          whether the current element is the last element
   */
  void setLastElement(boolean isLastElement)
  {
    toLastPositionButton.setVisible(!isLastElement);
    downButton.setVisible(!isLastElement);
  }



  /**
   * getter for the baseWidget property.
   * 
   * @return the baseWidget
   */
  public Widget getBaseWidget()
  {
    return baseWidget;
  }



  /**
   * Fired when the user clicks on a widget.
   * 
   * @param sender
   *          the widget sending the event.
   * @see com.google.gwt.user.client.ui.ClickListener#onClick(com.google.gwt.user.client.ui.Widget)
   */
  public void onClick(Widget sender)
  {

    final int myIndex = sortableVerticalPanel.getWidgetIndex(this);
    if (sender == toFirstPositionButton) {
      sortableVerticalPanel.moveWidget(myIndex, 0);
      return;
    }
    if (sender == toLastPositionButton) {
      sortableVerticalPanel.moveWidget(myIndex, sortableVerticalPanel.getWidgetCount() - 1);
      return;
    }
    if (sender == upButton) {
      sortableVerticalPanel.moveWidget(myIndex, myIndex - 1);
      return;
    }
    if (sender == downButton) {
      sortableVerticalPanel.moveWidget(myIndex, myIndex + 1);
      return;
    }
  }
}
